<?php

namespace plugin\ycMassage\app\model;

use plugin\ycMassage\utils\enum\ActionIdentity;
use plugin\ycMassage\utils\enum\State;
use think\Exception;
use think\facade\Db;

class MassageUserAgent extends Model
{
    public static function onAfterRead($model)
    {
        $model->agent_level = '总代理';
        if ($model->area_id) {
            $model->agent_level = "{$model->area}代理";
        } elseif ($model->city_id) {
            $model->agent_level = "{$model->city}代理";
        } elseif ($model->province_id) {
            $model->agent_level = "{$model->province}代理";
        }
    }
    public static function commission(MassageAppointmentService $model, $options)
    {
        $MassageOrdersAddress = MassageOrdersAddress::where(['orders_id' => $model->orders_id])->find();
        $where = [];
        $where[] = [
            ['saas_appid', '=', $model->saas_appid],
            ['state', '=', State::YES['value']],
            ['province_id', '=', NULL],
            ['city_id', '=', NULL],
            ['area_id', '=', NULL]
        ];
        if ($MassageOrdersAddress) {
            $where[] = [
                ['saas_appid', '=', $model->saas_appid],
                ['state', '=', State::YES['value']],
                ['province_id', '=', $MassageOrdersAddress->province_id],
                ['city_id', '=', NULL],
                ['area_id', '=', NULL]
            ];
            $where[] = [
                ['saas_appid', '=', $model->saas_appid],
                ['state', '=', State::YES['value']],
                ['city_id', '=', $MassageOrdersAddress->city_id],
                ['area_id', '=', NULL]
            ];
            $where[] = [
                ['saas_appid', '=', $model->saas_appid],
                ['state', '=', State::YES['value']],
                ['area_id', '=', $MassageOrdersAddress->area_id]
            ];
        }
        $Agents = MassageUserAgent::whereOr($where)->select();
        if ($Agents->isEmpty()) {
            return;
        }
        $agentConfig = getHpConfig('', $model->saas_appid, 'agent');
        foreach ($Agents as $item) {
            if ($item->commission && $item->commission > 0) {
                $commission = round($options['price'] * $item->commission / 100, 4);
            } else {
                if ($item->area_id) {
                    if (isset($agentConfig['area_agent_commission']) && $agentConfig['area_agent_commission'] > 0) {
                        $commission = round($options['price'] * $agentConfig['area_agent_commission'] / 100, 4);
                    } else {
                        continue;
                    }
                } elseif ($item->city_id) {
                    if (isset($agentConfig['city_agent_commission']) && $agentConfig['city_agent_commission'] > 0) {
                        $commission = round($options['price'] * $agentConfig['city_agent_commission'] / 100, 4);
                    } else {
                        continue;
                    }
                } elseif ($item->province_id) {
                    if (isset($agentConfig['province_agent_commission']) && $agentConfig['province_agent_commission'] > 0) {
                        $commission = round($options['price'] * $agentConfig['province_agent_commission'] / 100, 4);
                    } else {
                        continue;
                    }
                } else {
                    if (isset($agentConfig['agent_commission']) && $agentConfig['agent_commission'] > 0) {
                        $commission = round($options['price'] * $agentConfig['agent_commission'] / 100, 4);
                    } else {
                        continue;
                    }
                }
            }
            if ($item->province_id || $item->city_id || $item->area_id) {
                $remarks = "来自{$item->province}{$item->city}{$item->area}订单号：{$options['trade']}";
            } else {
                $remarks = "来自总代理订单号：{$options['trade']}";
            }
            if ($options['transaction_freeze_period']) {
                $MassageUserWallet = MassageUserWallet::where(['uid' => $item->uid])->lock(true)->find();
                if (!$MassageUserWallet) {
                    throw new Exception('用户钱包不存在');
                }
                $MassageUserFreezeMoney = new MassageUserFreezeMoney();
                $MassageUserFreezeMoney->uid = $item->uid;
                $MassageUserFreezeMoney->form_uid = $model->id;
                $MassageUserFreezeMoney->saas_appid = $model->saas_appid;
                $MassageUserFreezeMoney->action = ActionIdentity::AGENT['value'];
                $MassageUserFreezeMoney->money = $commission;
                $MassageUserFreezeMoney->orders_id = $model->orders_id;
                $MassageUserFreezeMoney->state = State::NO['value'];
                $MassageUserFreezeMoney->unfreezing_time = date('Y-m-d H:i:s', strtotime("+{$options['transaction_freeze_period']} hours"));
                $MassageUserFreezeMoney->remarks = $remarks;
                $MassageUserFreezeMoney->save();
                $MassageUserWallet->agent_freeze_money = Db::raw('agent_freeze_money+' . $commission);
                $MassageUserWallet->save();
            } else {
                MassageUserAgentBill::increase($remarks, $item->uid, $commission, $model->id);
            }
            $MassageUserAgentStatistics = MassageUserAgentStatistics::where(['saas_appid' => $model->saas_appid, 'agent_id' => $item->id])->whereDay('create_time')->find();
            if (!$MassageUserAgentStatistics) {
                $MassageUserAgentStatistics = new MassageUserAgentStatistics;
                $MassageUserAgentStatistics->uid = $item->uid;
                $MassageUserAgentStatistics->saas_appid = $model->saas_appid;
                $MassageUserAgentStatistics->agent_id = $item->id;
            }
            foreach ($options['statistics'] as $key => $value) {
                if ($value) {
                    $MassageUserAgentStatistics->{$key} = Db::raw("{$key}+{$value}");
                }
            }
            $MassageUserAgentStatistics->commission = Db::raw("commission+{$commission}");
            $MassageUserAgentStatistics->save();
        }
    }
}
